package com.chcit.share.admin.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chcit.share.admin.dao.SysDictItemMapper;
import com.chcit.share.admin.dao.SysDictMapper;
import com.chcit.share.admin.model.SysDictItem;
import com.chcit.share.admin.service.ISysDictItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.chcit.share.admin.model.SysDict;
import com.chcit.share.admin.service.SysDictService;
import org.springframework.transaction.annotation.Transactional;

/**
 * ---------------------------
 * 字典表 (SysDictServiceImpl)         
 * ---------------------------
 * 作者：  share-generator
 * 时间：  2019-04-05 01:48:06
 * 说明：  我是由代码生成器生生成的
 * ---------------------------
 */
@Service("sysDictServiceImpl")
@Slf4j
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

	@Autowired
	private SysDictMapper sysDictMapper;
	@Autowired
	private SysDictItemMapper sysDictItemMapper;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private ISysDictItemService sysDictItemService;

	/**
	 * 通过查询指定code 获取字典
	 * @param code
	 * @return
	 */
	@Override
	@Cacheable(value = "dictCache",key = "#code")
	public List<Map<String, Object>> queryDictItemsByCode(String code) {
		log.info("无缓存dictList的时候调用这里！");
		return sysDictMapper.queryDictItemsByCode(code);
	}

	/**
	 * 通过查询指定code 获取字典值text
	 * @param code
	 * @param key
	 * @return
	 */

	@Override
	@Cacheable(value = "dictCache")
	public String queryDictTextByKey(String code, String key) {
		log.info("无缓存dictText的时候调用这里！");
		return sysDictMapper.queryDictTextByKey(code, key);
	}

	/**
	 * 通过查询指定table的 text code 获取字典
	 * dictTableCache采用redis缓存有效期10分钟
	 * @param table
	 * @param text
	 * @param code
	 * @return
	 */
	@Override
	@Cacheable(value = "dictTableCache")
	public List<Map<String, Object>> queryTableDictItemsByCode(String table, String text, String code) {
		log.info("无缓存dictTableList的时候调用这里！");
		return sysDictMapper.queryTableDictItemsByCode(table,text,code);
	}

	/**
	 * 通过查询指定table的 text code 获取字典值text
	 * dictTableCache采用redis缓存有效期10分钟
	 * @param table
	 * @param text
	 * @param code
	 * @param key
	 * @return
	 */
	@Override
	@Cacheable(value = "dictTableCache")
	public String queryTableDictTextByKey(String table,String text,String code, String key) {
		log.info("无缓存dictTable的时候调用这里！");
		return sysDictMapper.queryTableDictTextByKey(table,text,code,key);
	}

	/**
	 * 根据字典类型id删除关联表中其对应的数据
	 */
	@Override
	public boolean deleteByDictId(SysDict sysDict) {
		sysDict.setDelFlag(2);
		sysDictService.updateById(sysDict);
		// 删除关联表中的数据
		String deleteId = sysDict.getId();
		LambdaQueryWrapper<SysDictItem> dictItemQuery = new LambdaQueryWrapper<SysDictItem>();
		dictItemQuery.eq(SysDictItem::getDictId, deleteId);
		boolean ok = sysDictItemService.remove(dictItemQuery);
		return ok;
	}

	@Override
	@Transactional
	public void saveMain(SysDict sysDict, List<SysDictItem> sysDictItemList) {

		sysDictMapper.insert(sysDict);
		if (sysDictItemList != null) {
			for (SysDictItem entity : sysDictItemList) {
				entity.setDictId(sysDict.getId());
				sysDictItemMapper.insert(entity);
			}
		}
	}
}
